package com.definesys.smecrailtransit.module.Navigation;


import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.definesys.smecrailtransit.MainActivity;
import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.SmecManager.SmecDialog.SmecDialog;
import com.definesys.smecrailtransit.SmecManager.SmecSession.SmecSession;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.base.SmecBaseActivity;
import com.definesys.smecrailtransit.databinding.ActivityRenyanBinding;
import com.definesys.smecrailtransit.module.Login.LoginActivity;
import com.definesys.smecrailtransit.receiver.JPushReceiver;
import com.definesys.smecrailtransit.receiver.receiverBean.JPushExtras;
import com.definesys.smecrailtransit.utils.ThreadPoolExecutorUtil;
import com.definesys.smecrailtransit.widget.TopBarLayout;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.github.mzule.activityrouter.annotation.Router;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import static com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment.JPUSH_LOAD_MONTH_FORM;
import static com.definesys.smecrailtransit.module.Navigation.NavigationAdapter.NavigationAdapter.NAVIGATION;
import static com.definesys.smecrailtransit.module.TotalTime.TotalTimeFragment.JPUSH_LOAD_RUN_TIME;
import static com.definesys.smecrailtransit.receiver.JPushReceiver.ACTIVITY_OPENDED;


/**
 * Created by xupeizuo on 2017/9/1.
 */
@Router("RenYanActivity")
public class RenYanActivity extends SmecBaseActivity{

    private ActivityRenyanBinding activityRenyanBinding;
    private NavigationViewModel navigationViewModel;
    private int currentPage=0;
    private NormalDialog normalDialog;

    @Override
    public Object getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityRenyanBinding=DataBindingUtil.setContentView(this, R.layout.activity_renyan);
        _initView();
    }

    /**
     * 初始化界面数据
     */
    private void _initView(){
        navigationViewModel=new NavigationViewModel();
        _initMsg();
        ThreadPoolExecutorUtil.getFixedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                RenYanActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        activityRenyanBinding.topBarLayout.setTopBarListener(new TopBarLayout.TopBarListener() {
                            @Override
                            public void setOnLeftClickListener() {
                                finish();
                            }

                            @Override
                            public void setOnRight1ClickListener() {

                            }

                            @Override
                            public void setOnRight2ClickListener() {

                            }
                        });
                        activityRenyanBinding.loginOut.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(normalDialog == null){
                                    normalDialog = SmecDialog.getInstance(RenYanActivity.this,"退出登录");

                                    normalDialog.setOnBtnClickL(
                                            new OnBtnClickL() {
                                                @Override
                                                public void onBtnClick() {
                                                    normalDialog.dismiss();
                                                }
                                            },
                                            new OnBtnClickL() {
                                                @Override
                                                public void onBtnClick() {
                                                    normalDialog.dismiss();
                                                    activityRenyanBinding.getRoot().postDelayed(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            mRxBus.post(LoginActivity.LOGIN_SEWITCH,new Object());
                                                        }
                                                    },300);
                                                }
                                            });
                                }
                                normalDialog.show();
                            }
                        });
                    }
                });
            }
        });
    }

    /**
     * 初始化页面数据
     */
    private void _initMsg(){
        navigationViewModel.setWeek(SmecRailConstant.getWeek());
        navigationViewModel.setUsername(SmecSession.getSmecUser().getUserName());
        String today=SmecRailConstant.getToday().format(new Date());
        String[] arr=today.split("-");
        navigationViewModel.setDay(arr[2]);
        navigationViewModel.setYearMonth(arr[1]+"/"+arr[0]);
        navigationViewModel.setAmOrPm(SmecRailConstant.getAmOrPm());
        activityRenyanBinding.setNaviModel(navigationViewModel);
    }

    /**
     * 点击事件
     * @param view
     */
    public void naviClick(View view){
        switch (view.getId()){
            case R.id.real_msg:
                currentPage=0;
                break;
            case R.id.site_details:
                currentPage=1;
                break;
            case R.id.real_status:
                currentPage=2;
                break;
            case R.id.fault_st:
                currentPage=3;
                break;
            case R.id.operator_status:
                currentPage=4;
                break;
            case R.id.line_report:
                currentPage=5;
                break;
        }
        Intent intent=new Intent(RenYanActivity.this, MainActivity.class);

        String[] param = new String[3];
        param[0] = currentPage+ "";
        param[1] = "";
        param[2] = "";

        intent.putExtra(NAVIGATION, param);
        startActivity(intent);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(ACTIVITY_OPENDED)
            }
    )
    public void openedActivity(JPushExtras jPushExtras){
        String funName = jPushExtras.getFunName();

        if(SmecRailConstant.isForeground(RenYanActivity.this,"com.definesys.smecrailtransit.module.Navigation.RenYanActivity")){
            if(funName !=null && !funName.equals("")){
                if(funName.equals(JPushReceiver.JPUSH_TYPE_REAL)){
                    currentPage=0;
                }
                if(funName.equals(JPushReceiver.JPUSH_TYPE_REPORT)){
                    currentPage=1;
                }
                if(funName.equals(JPushReceiver.JPUSH_TYPE_STATISTICS)){
                    currentPage=3;
                }
                if(funName.equals(JPushReceiver.JPUSH_TYPE_RUNTIME)){
                    currentPage=4;
                }
                if(funName.equals(JPushReceiver.JPUSH_TYPT_LINE_REPORT)){
                    currentPage=5;
                }

                Intent intent=new Intent(RenYanActivity.this, MainActivity.class);

                String[] param = new String[3];
                param[0] = currentPage+ "";
                param[1] = jPushExtras.getFrom();
                param[2] = jPushExtras.getTo();

                intent.putExtra(NAVIGATION, param);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onResume();
        _initMsg();
        activityRenyanBinding.invalidateAll();
    }
}
